package com.wjk.easy.view;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.wjk.easy.R;
import com.wjk.easy.db.Exam;
import com.wjk.easy.utils.ScreenUtil;

import java.util.Date;

public class CreateExamDialog extends Dialog {

    Activity context;
    private TextView btn_save;
    private EditText text_name;
    private EditText text_group_num;
    private EditText text_info;
    private View.OnClickListener mClickListener;

    public CreateExamDialog(Activity context) {
        super(context);
        this.context = context;
    }

    public CreateExamDialog(Activity context, int theme, View.OnClickListener clickListener) {
        super(context, theme);
        this.context = context;
        this.mClickListener = clickListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 指定布局
        this.setContentView(R.layout.dialog_add_exam);

        text_name = (EditText) findViewById(R.id.text_name);
        text_group_num = (EditText) findViewById(R.id.text_group_num);
        text_info = (EditText) findViewById(R.id.text_info);

        /*
         * 获取圣诞框的窗口对象及参数对象以修改对话框的布局设置, 可以直接调用getWindow(),表示获得这个Activity的Window
         * 对象,这样这可以以同样的方式改变这个Activity的属性.
         */
        Window dialogWindow = this.getWindow();

        WindowManager m = context.getWindowManager();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        //p.height = (int) (ScreenUtil.getScreenHeight(context) * 0.7); // 高度设置为屏幕的0.6
        p.width = (int) (ScreenUtil.getScreenWidth(context) * 0.5);
        dialogWindow.setAttributes(p);
        dialogWindow.setBackgroundDrawableResource(R.drawable.bg_dialog);
        // 根据id在布局中找到控件对象
        btn_save = (TextView) findViewById(R.id.tv_save);

        // 为按钮绑定点击事件监听器
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name = text_name.getText().toString().trim();
                String numStr = text_group_num.getText().toString();
                String remark = text_info.getText().toString();

                if(name.length()<1){
                    Toast.makeText(context,"请填写实验名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(numStr.length()<1){
                    numStr="1";
                }

                int groupNum = Integer.parseInt(numStr);

                Exam data = new Exam(name,groupNum,new Date(),remark);
                view.setTag(data);
                mClickListener.onClick(view);
                CreateExamDialog.this.dismiss();
            }
        });

        this.setCancelable(true);
    }
}
